package com.project.platform.controller;

import com.project.platform.entity.User;
import com.project.platform.service.UserService;
import com.project.platform.vo.PageVO;
import com.project.platform.vo.ResponseVO;
import jakarta.annotation.Resource;
import jakarta.websocket.server.PathParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

  @Resource
  private UserService userService;

  /**
   * 列表
   * @return
   */
  @GetMapping("list")
  public ResponseVO<List<User>> list() {
    return ResponseVO.ok(userService.list());
  }

  /**
   * 分页查询
   * @param query
   * @return
   */
  @GetMapping("page")
  public ResponseVO<PageVO<User>> page(@RequestParam Map<String, Object> query,
                                       @RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize
  ) {
    return ResponseVO.ok(userService.page(pageNum, pageSize, query));
  }

  /**
   * 根据id查询
   * @param id
   * @return
   */
  @GetMapping("selectById/{id}")
  public ResponseVO<User> selectById(@PathVariable("id") Integer id) {
    return ResponseVO.ok(userService.selectById(id));
  }
  /**
   * 根据用户名查询
   * @param username
   * @return
   */
  @GetMapping("selectByUsername/{username}")
  public ResponseVO<User> selectByUsername(@PathVariable("username") String username) {
    return ResponseVO.ok(userService.selectByUsername(username));
  }
  /**
   * 根据电话查询
   * @param tel
   * @return
   */
  @GetMapping("selectByTel/{tel}")
  public ResponseVO<User> selectByTel(@PathVariable("tel") String tel) {
    return ResponseVO.ok(userService.selectByTel(tel));
  }

  @PostMapping("add")
  public ResponseVO add(@RequestBody User user) {
    userService.insert(user);
    return ResponseVO.ok();
  }

  @PutMapping("update")
  public ResponseVO update(@RequestBody User user) {
    userService.updateById(user);
    return ResponseVO.ok();
  }

  @DeleteMapping("delBatch")
  public ResponseVO delBatch(@RequestBody List<Integer> ids) {
    userService.removeByIds(ids);
    return ResponseVO.ok();
  }
}
